﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Linq.Expressions;
using System.Web.UI;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;

namespace PUBCOM
{
    /// <summary>
    /// 抽象类
    /// </summary>
    public abstract class ICBST
    {
        #region 是否存在该记录
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        /// <returns>是否存在 True存在 False不存在</returns>
        public virtual bool Exists<T>(LambdaExpression fun) where T : class, new()
        {
            return new UEntity<T>().Exists(fun);
        }
        #endregion

        #region 插入
        /// <summary>
        /// 插入Model数据
        /// </summary>
        /// <param name="model">Model</param>
        /// <returns>插入结果 大于0为插入成功</returns>
        public virtual int Add<T>(T model) where T : class, new()
        {
            return new UEntity<T>().Insert(model);
        }
        #endregion

        #region 插入多条数据
        /// <summary>
        /// 插入多条数据
        /// </summary>
        /// <param name="model"></param>
        /// <param name="stamdlList"></param>
        /// <returns></returns>
        public int Add<T>(List<T> list) where T : class, new()
        {
            List<SQLObject> _TSQL = new List<SQLObject>();
            foreach (T model in list)
            {
                _TSQL.Add(CreateTxSQL.IC_Insert_SQL(model));
            }
            return DbHelperSQL.ExecuteSqlTran2(_TSQL, 1, false);
        }
        #endregion

        #region 更新
        /// <summary>
        /// 更新Model数据
        /// </summary>
        /// <param name="model">Model</param>
        /// <returns>更新结果 大于0为成功</returns>
        public virtual int Update<T>(T model) where T : class, new()
        {
            return new UEntity<T>().Update(model);
        }
        #endregion

        #region 更新多条数据
        /// <summary>
        /// 更新多条数据
        /// </summary>
        /// <param name="model"></param>
        /// <param name="stamdlList"></param>
        /// <returns></returns>
        public int Update<T>(List<T> list) where T : class, new()
        {
            List<SQLObject> _TSQL = new List<SQLObject>();
            foreach (T model in list)
            {
                _TSQL.Add(CreateTxSQL.IC_Update_SQL(model));
            }
            return DbHelperSQL.ExecuteSqlTran2(_TSQL, 1, false);
        }
        #endregion

        #region 删除
        /// <summary>
        /// 逻辑删除Model数据
        /// </summary>
        /// <param name="model">Model</param>
        /// <returns>删除结果 大于0为成功</returns>
        public virtual int Delete<T>(T model) where T : class, new()
        {
            return new UEntity<T>().Update(model);
        }

        /// <summary>
        /// 根据ID删除数据
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns>删除结果 大于0为成功</returns>
        public virtual int Delete<T>(object id) where T : class, new()
        {
            return new UEntity<T>().Delete(id);
        }

        /// <summary>
        /// 物理删除符合Expression条件表达式的数据
        /// </summary>
        /// <param name="fun">Expression条件表达式</param>
        /// <returns>删除结果 大于0为成功</returns>
        public virtual int Delete<T>(LambdaExpression fun) where T : class, new()
        {
            return new UEntity<T>().Delete(fun);
        }
        #endregion

        #region 获取单条详细信息
        /// <summary>
        /// 根据内部主键ID，获取单条详细信息
        /// </summary>
        /// <param name="ID">内部主键ID</param>
        /// <returns>Model，失败返回Null</returns>
        public virtual T GetDetail<T>(object ID) where T : class, new()
        {
            return new UEntity<T>().GetById(ID);
        }

        /// <summary>
        /// 根据指定字段，获取单条详细信息
        /// </summary>
        ///<param name="val">字段值</param>
        ///<param name="filed">字段名</param>
        /// <returns>Model，失败返回Null</returns>
        public virtual T GetDetail<T>(object val, string filed) where T : class, new()
        {
            return new UEntity<T>().GetById(val, filed);
        }

        /// <summary>
        /// 根据检索条件，获取单条详细信息
        /// </summary>
        ///<param name="lambda">条件表达式</param>
        /// <returns>Model，失败返回Null</returns>
        public virtual T GetDetail<T>(LambdaExpression lambda) where T : class, new()
        {
            return new UEntity<T>().GetByWhere(lambda);
        }
        #endregion

        #region 分页查询
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="qp">分页信息</param>
        /// <param name="RecordCount">返回记录数</param>
        /// <returns>返回数据表DataTable</returns>
        public virtual DataTable GetList(QueryParam qp, out int RecordCount)
        {
            SQLObject sql = CreateTxSQL.IC_Select_SQL(qp);
            DataSet ds = DbHelperSQL.RunProcedure(sql.TSQL, sql.Parms, "GetListPage");
            RecordCount = DataConverter.CLng(ds.Tables["GetListPage1"].Rows[0]["RecordCount"].ToString());
            return ds.Tables["GetListPage"];
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="qp">分页信息</param>
        /// <returns>返回数据表DataTable</returns>
        public virtual DataTable GetPageList(string sql, SortedList SDList, out int RecordCount)
        {
            QueryParam qp = new QueryParam();
            qp.SQLCONTENT = DbHelperSQL.ReplaceSQLParm(sql, SDList);
            qp.PageSize = DataConverter.CLng(SDList["@CURRPAGESIZE"]);
            qp.PageIndex = DataConverter.CLng(SDList["@CURRPAGE"]);
            return GetPageList(qp, out RecordCount);
        }
        public virtual DataTable GetPageList(QueryParam qp, out int RecordCount)
        {
            SQLObject sql = CreateTxSQL.IC_Select_SQL1(qp);
            DataSet ds = DbHelperSQL.RunProcedure(sql.TSQL, sql.Parms, "GetListPage");
            RecordCount = DataConverter.CLng(ds.Tables["GetListPage1"].Rows[0]["RecordCount"].ToString());
            return ds.Tables["GetListPage"];
        }
        

        /// <summary>
        /// 分页查询
        /// </summary>
        ///<param name="qp">分页信息</param>
        ///<param name="model">查询实体条件</param>
        ///<param name="RecordCount">返回记录数</param>
        public virtual IList<T> GetList<T>(QueryParam qp, T model, out int RecordCount) where T : class, new()
        {
            return new UEntity<T>().GetList(qp, model, out RecordCount);
        }

        /// <summary>
        /// 分页查询（不使用）
        /// </summary>
        /// <param name="qp">分页信息</param>
        /// <param name="lambda">lambda表达式</param>
        /// <param name="RecordCount">返回记录数</param>
        /// <returns>返回数据表DataTable</returns>
        public virtual DataTable GetList<T>(QueryParam qp, LambdaExpression lambda, out int RecordCount) where T : class, new()
        {
            return new UEntity<T>().GetList(qp, lambda, out RecordCount);
        }


        #endregion

        #region 检索
        /// <summary>
        /// 根据条件返回数据（不使用）
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="lambda">Lambda表达式(条件表达式)</param>
        /// <returns></returns>
        public virtual IList<T> GetList<T>(LambdaExpression lambda) where T : class, new()
        {
            return new UEntity<T>().GetList(lambda);
        }

        /// <summary>
        /// 根据条件返回数据（不使用）
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="lambda">Lambda表达式(条件表达式)</param>
        /// <returns></returns>
        public virtual DataTable D_GetList<T>(LambdaExpression lambda) where T : class, new()
        {
            return new UEntity<T>().D_GetList(0, lambda);
        }

        /// <summary>
        /// 返回指定记录数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="TOP">0为全部</param>
        /// <returns></returns>
        public virtual IList<T> GetList<T>(int TOP) where T : class, new()
        {
            return new UEntity<T>().GetTopList(TOP);
        }

        /// <summary>
        ///  返回指定记录数（不使用）
        /// </summary>
        /// <param name="top"></param>
        /// <param name="lambda"></param>
        /// <returns></returns>
        public virtual IList<T> GetList<T>(int top, LambdaExpression lambda) where T : class, new()
        {
            return new UEntity<T>().GetList(top, lambda);
        }

        /// <summary>
        ///  返回指定记录数（不使用）
        /// </summary>
        /// <param name="top"></param>
        /// <param name="lambda"></param>
        /// <returns></returns>
        public virtual DataTable D_GetList<T>(int top, LambdaExpression lambda) where T : class, new()
        {
            return new UEntity<T>().D_GetList(top, lambda);
        }

        /// <summary>
        /// 返回查询数据（不使用）
        /// </summary>
        ///<param name="fileds">查询字段</param>
        ///<param name="exp">查询条件</param>
        ///<param name="parms">查询表</param>
        public DataTable GetList<T>(string[] fileds, LambdaExpression lambda) where T : class, new()
        {
            return new UEntity<T>().GetList(fileds, lambda);
        }

        /// <summary>
        /// 返回查询数据(可排序)（不使用）
        /// </summary>
        ///<param name="fileds">查询字段</param>
        ///<param name="exp">查询条件</param>
        ///<param name="parms">查询表</param>
        public DataTable GetList<T>(string returnField, LambdaExpression lambda, string orderField) where T : class, new()
        {
            return new UEntity<T>().GetList(returnField, lambda, orderField);
        }
        #endregion

        #region DataTalbe转化成对象列表
        /// <summary>
        /// 返回列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        public List<T> ToList<T>(DataTable dt) where T : class, new()
        {
            return new UEntity<T>().ToList<T>(dt);
        }

        /// <summary>
        /// 把DataTable中的第一条数据转成对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        public T ToObject<T>(DataTable dt) where T : class, new()
        {
            if (dt.Rows.Count > 0)
            {
                T t = new T();
                new UEntity<T>().FillObject(dt.Rows[0], t);
                return t;
            }
            return null;
        }
        #endregion

    }
}
